package UI;

import java.awt.BorderLayout;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import logController.LogMessage;
import logController.Logger;

public class LogUI extends JPanel implements Observer{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 201204131035L;
	private JScrollPane jScrollLogText = new JScrollPane();
    private JTextArea jLogText = new JTextArea();
    private JLabel jName = new JLabel();
    
    public LogUI(){
    	
    	this.setLayout(new BorderLayout());
    	
    	jName.setText("Historial");
    	jScrollLogText.setViewportView(jLogText);
    	// No es editable
    	jLogText.setEditable(false);
    	
    	this.add(jName, BorderLayout.NORTH);
    	this.add(jScrollLogText, BorderLayout.CENTER); 
    	Logger.startLog();
    	Logger.addObserver(this);
    }
    
    //public void appendMessage(String msg){
    //	jLogText.append(msg);
    //}

	@Override
	/**
	 * @params log Un Objeto de la clase LOG
	 * @params arg1 parametros adicionales. NO SE UTILIZA
	 * 
	 */
	public void update(Observable log, Object arg1) {
		List<LogMessage> lista = Logger.getUnreadMessages();
		String res = "";
		
		for(LogMessage msg : lista){
			res += msg.toString()+'\n';
		}
		
		res = jLogText.getText() + res; 
		jLogText.setText(res);
	}
}
